package com.rest.core.aop;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 记录请求参数
 * 
 * @Description: Spring AOP方式记录REST请求的参数信息
 * @Package com.rest.core.aop.ApiParamsAop
 * @author Smile- 2016年4月13日 下午10:00:45
 */
@Component
@Aspect
public class ApiParamsAop {

	private Logger logger = Logger.getLogger(this.getClass());

	// 声明切入点
	@Pointcut("execution(* com.rest.api..*.*(..))")
	public void anyMethod() {
	}

	// 拦截对目标对象方法调用
	// 环绕
	@Around("anyMethod()")
	public Object doLoggerPointCut(ProceedingJoinPoint joinPoint)
			throws Throwable {
		System.out.println("环绕通知");
		// 获取连接点的方法签名对象
		MethodSignature joinPointObject = (MethodSignature) joinPoint
				.getSignature();
		Object[] args = joinPoint.getArgs();
		Object[] names = joinPointObject.getParameterNames();
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("类名：", joinPoint.getTarget().getClass());
		map.put("方法名：", joinPoint.getSignature().getName());
		for (int i = 0; i < args.length; i++) {
			map.put(names[i], args[i]);
		}
		logger.info(map);
		return joinPoint.proceed();
	}
}
